package com.shopowner.dao.impl;


import com.shopowner.page.PageInfo;
import com.shopowner.utils.GenNumTools;
import com.shopowner.dao.ShopownerDao;
import com.shopowner.entity.Shopowner;
import com.shopowner.mapper.ShopownerMapper;

import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("shopownerDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ShopownerDaoImpl implements ShopownerDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ShopownerMapper shopownerMapper;
    public List<Shopowner>  getAllShopowners() throws Exception{
        return shopownerMapper.selectByExample(null);
    }
    public Shopowner  selectShopownerBySno(String sno) throws Exception{
        return shopownerMapper.selectByPrimaryKey(sno);
    }

    @Override
    public Integer deleteShopownerBySno(String sno)  throws Exception{
        return shopownerMapper.deleteByPrimaryKey(sno);
    }

    @Override
    public Integer updateShopownerBySno(Shopowner shopowner)  throws Exception{
        return shopownerMapper.updateByPrimaryKey(shopowner);
    }

    @Override
    public Integer addShopowner(Shopowner shopowner)  throws Exception{
        shopowner.setSno(initId());
        return shopownerMapper.insertSelective(shopowner);
    }
    public String initId(){
        String maxSno = shopownerMapper.getMaxSno();
        return GenNumTools.initId("s",4,maxSno);
    }
    @Override
    public List<Shopowner> getShopownerByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)shopownerMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return shopownerMapper.getShopownerByPage(map);
    }


}
